/*
 * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.registry.custom.dia.ui.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.custom.dia.ui.beans.EndpointBean;
import org.wso2.carbon.registry.common.ui.UIException;
import org.wso2.carbon.registry.resource.ui.clients.CustomUIServiceClient;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.OMElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;
import javax.xml.namespace.QName;
import org.wso2.carbon.registry.custom.dia.ui.RegistryDiaAdminService;
import org.wso2.carbon.registry.custom.dia.ui.RegistryDiaAdminServiceStub;
import org.wso2.carbon.registry.custom.dia.ui.beans.xsd.TagBean;
import org.wso2.carbon.registry.custom.dia.ui.clients.TopicServiceClient;

public class GetDocumentUtil {

    private static final Log log = LogFactory.getLog(GetDocumentUtil.class);

    private String title,summary,externalLink;
 
    public EndpointBean getEndpointBean(
            String path, ServletConfig config, HttpSession session) throws UIException {
//TODO change onclick submission of form to action in form tag
        try {
            CustomUIServiceClient customUIServiceClient =
                    new CustomUIServiceClient(config, session);

            String content = customUIServiceClient.getTextContent(path);
            OMElement document = AXIOMUtil.stringToOM(content);

            /*Structure of the OM
             *
             * <document>
                  <title>xxx</title>
                  <summary>xxxx</summary>
                  <externalLink>http://xxx</externalLink> 
             * </document>
             */

            Iterator i = document.getChildElements();
            while(i.hasNext()){
                OMElement ome = (OMElement) i.next();
                        //ome- child OMElements of document
                String localName = ome.getLocalName(); //ex: title, tags
                processLocalName(localName, ome);
            }

            EndpointBean documentBean = new EndpointBean();
            documentBean.setTitle(title);
            documentBean.setSummary(summary);
            documentBean.setExternalLink(externalLink); 

            return documentBean;

        } catch (Exception e) {
            String msg = "Failed to get end point details. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }

    private void processLocalName(String localName, OMElement ome) {
        if(localName.equals("title")){
            title = ome.getText();
        } else if(localName.equals("summary")){
            summary = ome.getText();
        } else if(localName.equals("externalLink")){
            externalLink = ome.getText();
        } 
    }
//TODO remove this
    public void getTagsString(
            String path, ServletConfig config, HttpSession session) throws UIException { 
            try {
                TopicServiceClient client = new TopicServiceClient(config, session);
                TagBean[] tagBean = client.getTags(path);
                
            } catch (RegistryException ex) {
                Logger.getLogger(GetDocumentUtil.class.getName()).log(Level.SEVERE, null, ex);
                throw new UIException(ex);
            }
    }


}

